<?php

namespace app\api\controller;

use app\admin\model\Club;
use app\admin\model\ClubDtl;
use app\admin\model\TeacherArea;
use app\admin\model\TeacherCollection;
use app\admin\model\TeacherCourseDisplay;
use app\admin\model\TeacherDetail;
use app\common\controller\Api;

/**
 * 社团接口
 */
class Clubs extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    protected function formatScore($score)
    {
        if ($score === null || $score === '') {
            return $score;
        }
        if (is_numeric($score)) {
            $score = (float)$score;

            // 最简单最傻瓜式的判断
            // 小于等于0 -> 0
            if ($score <= 0) {
                return '0.0';
            }

            // 大于等于10 -> 5
            if ($score >= 10) {
                return '5.0';
            }

            // 其他情况：向下取整得到整数部分
            $intPart = (int)floor($score);

            // 判断是否为整数（分数的小数部分是否接近0）
            $isInteger = abs($score - $intPart) < 0.001;

//            if ($isInteger) {
//                // 整数：n * 0.5
//                // 1 -> 0.5, 2 -> 1.0, 3 -> 1.5, 4 -> 2.0, ...
                $formattedScore = $intPart * 0.5;
//            } else {
//                // 小数：(n+1) * 0.5
//                // 0.1-0.9 -> 0.5, 1.1-1.9 -> 1.0, 2.1-2.9 -> 1.5, ...
//                $formattedScore = ($intPart + 1) * 0.5;
//            }

            return number_format($formattedScore, 1, '.', '');
        }
        return $score;
    }
    //社团主页
    public function club_index(){
        $params = $this->request->param();
        if(!isset($params['page'])||!$params['page']){
            $params['page'] = 1;
        }
        if(!isset($params['page_size'])||!$params['page_size']){
            $params['page_size'] = 10;
        }
        if(!isset($params['club_id'])||!$params['club_id']){
            $this->error('社团id未传');
        }
        $dtl = ClubDtl::where(['club_id'=>$params['club_id']])->order('sort','asc')->select();
        $club_dtl_id = 0;
        if(isset($params['club_dtl_id'])&&$params['club_dtl_id']){
            $club_dtl_id = $params['club_dtl_id'];
        }
        $teacher_detail_id = 0;
        $remark = '';
        $data= [];
        //获取子社团分类
        $club_dtl_arr = [];
        foreach ($dtl as $k=>$v){
            if($club_dtl_id == 0){
                if($k == 0){
                    $teacher_detail_id = $v['teacher_detail_id'];
                    $remark = $v['remark'];
                    $club_dtl_id = $v['id'];
                }
            }else{
                if($club_dtl_id == $v['id']){
                    $teacher_detail_id = $v['teacher_detail_id'];
                    $remark = $v['remark'];
                }
            }
            $club_dtl_arr[$k]['id'] = $v['id'];
            $club_dtl_arr[$k]['name'] = $v['name'];
            $club_dtl_arr[$k]['img_url'] = $v['img_url'];
            $club_dtl_arr[$k]['stage'] = $v['stage'];
            $club_dtl_arr[$k]['grade'] = $v['grade'];
        }
        $data['club_dtl'] = $club_dtl_arr;

        //获取子社团专栏教师
        $club_dtl_teacher_top = [];
        $display = TeacherCourseDisplay::field('id,title,img')->where(['teacher_detail_id'=>$teacher_detail_id])->order('sort','desc')->select();

        // 只显示状态为1的课程和教师
        $td = TeacherDetail::where(['id'=>$teacher_detail_id,'detail_status'=>1])
            ->with(['identity'=>function($query){
                $query->where(['main_status'=>1]);
            },'clubDtlS'])
            ->find();
//        $identity_info = \app\admin\model\IdentityInfo::where(['user_id'=>$user_id,'identity_type'=>'教师'])->with(['user','clubDtlS'])->find();
        if($td && isset($td['identity']) && $td['identity']){
            $club_dtl_teacher_top['is_have'] = 1;
            $club_dtl_teacher_top['id'] = $td['id'];
            $club_dtl_teacher_top['user_id'] = $td['identity']['user_id'];
            $club_dtl_teacher_top['name'] = $td['identity']['name'];
            $club_dtl_teacher_top['avatar'] = $td['identity']['user']['avatar'];
            $club_dtl_teacher_top['course_num'] = count($display);
            $club_dtl_teacher_top['people'] = $td['people'];
            $club_dtl_teacher_top['score'] = ($td['score'] * 10) .'%';
            $club_dtl_teacher_top['remark'] = $remark;
            $club_dtl_teacher_top['course'] = $display;

            $area = $td['identity']['area'] ? explode(',',$td['identity']['area']) : [];
            $area_arr = TeacherArea::get_arr();
            $area_name = '';
            foreach ($area as $k=>$v){
                $area_name .= isset($area_arr[$v]) ? $area_arr[$v].',' : '';
            }
            $club_dtl_teacher_top['area_name'] = trim($area_name,',');
            $club_dtl_teacher_top['stage'] = isset($td['club_dtl_s']) ? $td['club_dtl_s']['stage'] : '';
            $club_dtl_teacher_top['grade'] = isset($td['club_dtl_s']) ? $td['club_dtl_s']['grade'] : '';
            $club_dtl_teacher_top['tag_me']    = $td['tag_me'];
        }else{
            $club_dtl_teacher_top['is_have'] = 0;
        }
        $data['club_dtl_teacher_top'] = $club_dtl_teacher_top;


        //教师
        $query_res = \app\admin\model\IdentityInfo::hasWhere('teacherDetail',function ($query)use($params,$club_dtl_id){
            // 只显示状态为1的课程
            $query->where(['fa_teacher_detail.detail_status'=>1]);
            if(isset($club_dtl_id)&&$club_dtl_id){
                $query->where(['fa_teacher_detail.club_two_id'=>$club_dtl_id]);
            }

            //工作经验
            if(isset($params['work_time'])&&$params['work_time']){
                switch ($params['work_time']){
                    case 1:
                        $work_time_sql = "work_time < 1";
                        break;
                    case 2:
                        $work_time_sql = "work_time >= 1 and work_time < 3";
                        break;
                    case 3:
                        $work_time_sql = "work_time >= 3 and work_time < 5";
                        break;
                    case 4:
                        $work_time_sql = "work_time >= 5 and work_time < 7";
                        break;
                    case 5:
                        $work_time_sql = "work_time >= 7 and work_time < 10";
                        break;
                    case 6:
                        $work_time_sql = "work_time >= 10 and work_time < 20";
                        break;
                    case 7:
                        $work_time_sql = "work_time >= 20 and work_time < 30";
                        break;
                    case 8:
                        $work_time_sql = "work_time >= 30";
                        break;
                    default:
                        $work_time_sql = '';
                        break;
                }
                if($work_time_sql){
                    $query->where($work_time_sql);
                }
            }
        });
        $query_res->with(['user','teacherDetail'=>function($query)use($club_dtl_id){
            // 只显示状态为1的课程
            $query->where(['club_two_id'=>$club_dtl_id,'detail_status'=>1]);
        }])->where(['identity_type'=>'教师','main_status'=>1]);

        //社团课程筛选
//        if(isset($params['club_one_id'])&&$params['club_one_id']){
//            $query_res->hasWhere('teacherDetail',['club_one_id'=>$params['club_one_id']]);
//        }
//        if(isset($params['club_two_id'])&&$params['club_two_id']){
//            $query_res->hasWhere('teacherDetail',['club_two_id'=>$params['club_two_id']]);
//        }
        //地区
        if(isset($params['area'])&&$params['area']){
            $query_res->whereRaw("FIND_IN_SET({$params['area']}, area) > 0");
        }
        //标签
        if(isset($params['tag'])&&$params['tag']){
            $query_res->whereRaw("FIND_IN_SET({$params['tag']}, tag) > 0");
        }
        //可约时间
        if(isset($params['appointment_time'])&&$params['appointment_time']){
            $appointmentTime = trim($params['appointment_time']);
            $query_time = null;
            if (preg_match('/^\d{1,2}:\d{1,2}(?::\d{1,2})?$/', $appointmentTime)) {
                $parts = explode(':', $appointmentTime);
                $hour = str_pad($parts[0], 2, '0', STR_PAD_LEFT);
                $minute = str_pad($parts[1] ?? '00', 2, '0', STR_PAD_LEFT);
                $second = str_pad($parts[2] ?? '00', 2, '0', STR_PAD_LEFT);
                $query_time = "{$hour}:{$minute}:{$second}";
            } else {
                $timestamp = strtotime($appointmentTime);
                if ($timestamp !== false) {
                    $query_time = date('H:i:s', $timestamp);
                }
            }
            if ($query_time) {
                $query_res->whereRaw("(TIME(SUBSTRING_INDEX(appointment_time, ' - ', 1)) <= '{$query_time}' AND 
    TIME(SUBSTRING_INDEX(appointment_time, ' - ', -1)) >= '{$query_time}')");
            }
        }
        //最高学历
        if(isset($params['education_background'])&&$params['education_background']){
            $query_res->where('fa_identity_info.education_background',$params['education_background']);
        }
        //金额
        if(isset($params['price'])&&$params['price']){
            switch ($params['price']){
                case 1:
                    $hourly_rate_sql = "lowest_hourly_rate < 200";
                    break;
                case 2:
                    $hourly_rate_sql = "lowest_hourly_rate >= 200 and lowest_hourly_rate < 300";
                    break;
                case 3:
                    $hourly_rate_sql = "lowest_hourly_rate >= 300 and lowest_hourly_rate < 400";
                    break;
                case 4:
                    $hourly_rate_sql = "lowest_hourly_rate >= 400 and lowest_hourly_rate < 500";
                    break;
                case 5:
                    $hourly_rate_sql = "lowest_hourly_rate >= 500 and lowest_hourly_rate < 700";
                    break;
                case 6:
                    $hourly_rate_sql = "lowest_hourly_rate >= 700 and lowest_hourly_rate < 1000";
                    break;
                case 7:
                    $hourly_rate_sql = "lowest_hourly_rate >= 1000 and lowest_hourly_rate < 2000";
                    break;
                case 8:
                    $hourly_rate_sql = "lowest_hourly_rate >= 2000";
                    break;
                default:
                    $hourly_rate_sql = '';
                    break;
            }
            if($hourly_rate_sql){
                $query_res->where($hourly_rate_sql);
            }
        }
        //性别
        if(isset($params['gender'])&&$params['gender']){
            if($params['gender'] == 1){
                $gender_user_id_arr = \app\admin\model\User::where(['gender'=>1])->column('id');
                $query_res->whereIn('user_id',$gender_user_id_arr);
            }
            if($params['gender'] == 2){
                $gender_user_id_arr = \app\admin\model\User::where(['gender'=>0])->column('id');
                $query_res->whereIn('user_id',$gender_user_id_arr);
            }
        }
        $query_res->where("lowest_hourly_rate > 0");
        if(isset($params['area_sort'])&&$params['area_sort']){
            //当前地址地区排序 - 包含传入值的排在最前面，其他按order_num排序
            $area_sort = (int)$params['area_sort']; // 转换为整数防止SQL注入
            $query_res = $query_res->orderRaw("FIND_IN_SET({$area_sort}, area)>0 DESC");
        }

        if(isset($params['sort_type'])&&$params['sort_type']){
            if($params['sort_type']!='desc'&&$params['sort_type']!='asc'&&$params['sort_type']!='DESC'&&$params['sort_type']!='ASC'){
                $this->error('排序方式参数错误');
            }
        }
        if(isset($params['sort_name'])&&$params['sort_name']){
            switch ($params['sort_name']){
                case "score":
                    if(isset($params['sort_type'])&&$params['sort_type']){
                        $query_res = $query_res->order('main_score',$params['sort_type']);
                    }else{
                        $query_res = $query_res->order('main_score','desc');
                    }
                    break;
                case "price":
                    if(isset($params['sort_type'])&&$params['sort_type']) {
                        $query_res = $query_res->order('lowest_hourly_rate',$params['sort_type']);
                    }else{
                        $query_res = $query_res->order('lowest_hourly_rate', 'desc');
                    }
                    break;
                default:
                    break;
            }
        }

        $query_res = $query_res->order('main_people','desc');
        $query_res = $query_res->paginate($params['page_size']);

        $list = $query_res->getCollection();


        // 获取当前登录用户（如果已登录）
        $current_user_id = null;
        try {
            $current_user = $this->auth->getUser();
            if (isset($current_user['id']) && $current_user['id']) {
                $current_user_id = $current_user['id'];
            }
        } catch (\Exception $e) {
            // 用户未登录，忽略
        }

        // 如果用户已登录，批量查询收藏状态
        $collected_teacher_detail_ids = [];
        if ($current_user_id) {
            // 收集所有teacher_detail_id
            $all_teacher_detail_ids = [];
            foreach ($list as $v) {
                foreach ($v['teacher_detail'] as $vv) {
                    if (isset($vv['id'])) {
                        $all_teacher_detail_ids[] = $vv['id'];
                    }
                }
            }
            // 批量查询收藏状态
            if (!empty($all_teacher_detail_ids)) {
                $collected_teacher_detail_ids = TeacherCollection::where(['user_id' => $current_user_id])
                    ->whereIn('teacher_detail_id', array_unique($all_teacher_detail_ids))
                    ->column('teacher_detail_id');
            }
        }

        $arr = [];
        foreach ($list as $k=>$v){
            $arr[$k]['id']     = $v['id'];
            $arr[$k]['user_id'] = $v['user_id'];
            $arr[$k]['name']     = $v['name'];
            $club_dtl_name = [];
            $teacher_detail = [];
            $minHourlyRate = null;
            foreach ($v['teacher_detail'] as $kk=>$vv){
                if(isset($vv['club_dtl_s']['name'])&&$vv['club_dtl_s']['name']){
                    $club_dtl_name[] = $vv['club_dtl_s']['name'];
                }
                $teacher_detail[$kk]['id'] = $vv['id'];
                $teacher_detail[$kk]['info_id'] = $vv['info_id'];
                $teacher_detail[$kk]['hourly_rate'] = $vv['hourly_rate'];
                $teacher_detail[$kk]['club_name'] = isset($vv['club_dtl_s']['name']) ? $vv['club_dtl_s']['name'] : '';
                $teacher_detail[$kk]['club_img_url'] = isset($vv['club_dtl_s']['img_url']) ? $vv['club_dtl_s']['img_url'] : '';
                $teacher_detail[$kk]['is_premium'] = $vv['is_premium'];
                $teacher_detail[$kk]['people'] = $vv['people'];
                $currentRate = is_numeric($vv['hourly_rate']) ? (float)$vv['hourly_rate'] : null;
                if ($currentRate !== null && $currentRate >= 0) {
                    if ($minHourlyRate === null || $currentRate < $minHourlyRate) {
                        $minHourlyRate = $currentRate;
                    }
                }
                if($vv['work_time']>0){
                    $teacher_detail[$kk]['work_time'] = $vv['work_time'].'年';
                }else{
                    $teacher_detail[$kk]['work_time'] = '';
                }
                // 检查该课程是否被收藏
                $is_collected = 2; // 默认2=未收藏
                if ($current_user_id && isset($vv['id']) && in_array($vv['id'], $collected_teacher_detail_ids)) {
                    $is_collected = 1; // 1=已收藏
                }
                $teacher_detail[$kk]['is_collected'] = $is_collected; // 1已收藏，2未收藏
            }
            $club_dtl_name = array_values(array_unique($club_dtl_name));
            $arr[$k]['club_dtl_name'] = $club_dtl_name;
            $arr[$k]['hourly_rate']    = $minHourlyRate !== null ? $minHourlyRate : $v['lowest_hourly_rate'];
            $arr[$k]['score']    = $this->formatScore($v['main_score']);
            $arr[$k]['people']    = $v['people'];
            $arr[$k]['order_num']    = $v['order_num_all'];
            $arr[$k]['education_background']    = $v['education_background'];
            $week = isset($v['appointment_time_week']) ? $v['appointment_time_week'] : '';
            $time = isset($v['appointment_time']) ? $v['appointment_time'] : '';
            $arr[$k]['appointment_time_week']    = $week.' '.$time;
            $arr[$k]['appointment_time']    = $time;
            $arr[$k]['appointment_week']    = $week;
            $arr[$k]['avatar']    = $v['user']['avatar'];

            $area = $v['area'] ? explode(',',$v['area']) : [];
            $area_arr = TeacherArea::get_arr();
            $area_name = '';
            foreach ($area as $kk=>$vv){
                $area_name .= isset($area_arr[$vv]) ? $area_arr[$vv].',' : '';
            }
            $arr[$k]['area_name'] = trim($area_name,',');
            $arr[$k]['tag_me']    = $v['tag_me'];
            $arr[$k]['dtl']    = $teacher_detail;
        }

        $count = $query_res->total();
        $data['teacher_list'] = $arr;
        $data['teacher_count'] = $count;
        $this->success('社团主页',$data);

        exit;

        $query_res = TeacherDetail::with(['identity','club_dtl'])->where('info_id > 0')->where(['club_two_id'=>$club_dtl_id]);
        //地区
        if(isset($params['area'])&&$params['area']){
            $query_res->whereRaw("FIND_IN_SET({$params['area']}, area) > 0");
        }
        //标签
        if(isset($params['tag'])&&$params['tag']){
            $query_res->whereRaw("FIND_IN_SET({$params['tag']}, tag) > 0");
        }
        //可约时间
        if(isset($params['appointment_time'])&&$params['appointment_time']){
            $info_time = \app\admin\model\IdentityInfo::whereRaw("(TIME(SUBSTRING_INDEX(appointment_time, ' - ', 1)) > '{$params['appointment_time']}' AND 
     TIME(SUBSTRING_INDEX(appointment_time, ' - ', -1)) < '{$params['appointment_time']}')")->column('id');
            $query_res->whereIn('info_id',$info_time);
        }
        //工作经验
        if(isset($params['work_time'])&&$params['work_time']){
            switch ($params['work_time']){
                case 1:
                    $work_time_sql = "work_time < 1";
                    break;
                case 2:
                    $work_time_sql = "work_time >= 1 and work_time < 3";
                    break;
                case 3:
                    $work_time_sql = "work_time >= 3 and work_time < 5";
                    break;
                case 4:
                    $work_time_sql = "work_time >= 5 and work_time < 7";
                    break;
                case 5:
                    $work_time_sql = "work_time >= 7 and work_time < 10";
                    break;
                case 6:
                    $work_time_sql = "work_time >= 10 and work_time < 20";
                    break;
                case 7:
                    $work_time_sql = "work_time >= 20 and work_time < 30";
                    break;
                case 8:
                    $work_time_sql = "work_time >= 30";
                    break;
                default:
                    $work_time_sql = '';
                    break;
            }
            if($work_time_sql){
                $query_res->where($work_time_sql);
            }
        }
        //最高学历
        if(isset($params['education_background'])&&$params['education_background']){
            $education_background_arr_id = \app\admin\model\IdentityInfo::where(['education_background'=>$params['education_background']])->column('id');
            $query_res->whereIn('info_id',$education_background_arr_id);
        }
        //金额
        if(isset($params['price'])&&$params['price']){
            switch ($params['price']){
                case 1:
                    $hourly_rate_sql = "hourly_rate < 200";
                    break;
                case 2:
                    $hourly_rate_sql = "hourly_rate >= 200 and hourly_rate < 300";
                    break;
                case 3:
                    $hourly_rate_sql = "hourly_rate >= 300 and hourly_rate < 400";
                    break;
                case 4:
                    $hourly_rate_sql = "hourly_rate >= 400 and hourly_rate < 500";
                    break;
                case 5:
                    $hourly_rate_sql = "hourly_rate >= 500 and hourly_rate < 700";
                    break;
                case 6:
                    $hourly_rate_sql = "hourly_rate >= 700 and hourly_rate < 1000";
                    break;
                case 7:
                    $hourly_rate_sql = "hourly_rate >= 1000 and hourly_rate < 2000";
                    break;
                case 8:
                    $hourly_rate_sql = "hourly_rate >= 2000";
                    break;
                default:
                    $hourly_rate_sql = '';
                    break;
            }
            if($hourly_rate_sql){
                $query_res->where($hourly_rate_sql);
            }
        }
        //性别
        if(isset($params['gender'])&&$params['gender']){
            $gender_user_id_arr = \app\admin\model\User::where(['gender'=>$params['gender']])->column('id');
            $gender_info_id_arr = \app\admin\model\IdentityInfo::where(['identity_type'=>'教师'])->whereIn('user_id',$gender_user_id_arr)->column('id');
            $query_res->whereIn('info_id',$gender_info_id_arr);
        }

        //排序
        if(isset($params['score'])&&$params['score']){
            //评分排序
            $query_res = $query_res->order('score','desc')->order('order_num','desc');
        }else{
            if(isset($params['area_sort'])&&$params['area_sort']){
                //当前地址地区排序 - 包含传入值的排在最前面，其他按order_num排序
                $area_sort = (int)$params['area_sort']; // 转换为整数防止SQL注入
                $info_order = \app\admin\model\IdentityInfo::where(['identity_type'=>'教师'])->whereRaw("FIND_IN_SET({$area_sort}, area)")->column('id');

                // 使用FIELD函数让搜索出来的id对应的记录排在前面
                if (!empty($info_order)) {
                    // 过滤数组中的值，确保都是整数，防止SQL注入
                    $info_order = array_map('intval', $info_order);
                    $info_order_ids = implode(',', $info_order);
                    // FIELD(info_id, id1, id2, ...) DESC: 在列表中的返回位置(>0)排在前面，不在列表中的返回0排在后面
                    $query_res = $query_res->orderRaw("FIELD(info_id, {$info_order_ids}) DESC, order_num DESC,score DESC");
                } else {
                    // 如果没有匹配的，直接按order_num排序
                    $query_res = $query_res->order('order_num', 'desc')->order('score','desc');
                }
            }else{
                //订单排序
                $query_res = $query_res->order('order_num','desc')->order('score','desc');
            }
        }

        $query_res = $query_res->paginate($params['page_size']);
        $list = $query_res->getCollection();
        
        // 获取当前登录用户（如果已登录）
        $current_user_id = null;
        try {
            $current_user = $this->auth->getUser();
            if (isset($current_user['id']) && $current_user['id']) {
                $current_user_id = $current_user['id'];
            }
        } catch (\Exception $e) {
            // 用户未登录，忽略
        }
        
        $arr = [];
        foreach ($list as $k=>$v){
            $arr[$k]['id']     = $v['id'];
            $arr[$k]['user_id'] = $v['identity']['user_id'];
            $arr[$k]['name']     = $v['identity']['name'];
            $arr[$k]['club_dtl_name'] = $v['club_dtl']['name'];
            $arr[$k]['hourly_rate']    = $v['hourly_rate'];
            $arr[$k]['score']    = $v['score'];
            $arr[$k]['people']    = $v['people'];
            $arr[$k]['order_num']    = $v['order_num'];
            $arr[$k]['is_premium']    = $v['is_premium'];
            $arr[$k]['appointment_time_week']    = $v['identity']['appointment_time_week'].' '.$v['identity']['appointment_time'];
            $arr[$k]['appointment_time']    = $v['identity']['appointment_time'];
            $arr[$k]['appointment_week']    = $v['identity']['appointment_time_week'];
            $arr[$k]['avatar']    = $v['identity']['user']['avatar'];

            $arr[$k]['stage'] = isset($v['club_dtl']) ? $v['club_dtl']['stage'] : '';
            $arr[$k]['grade'] = isset($v['club_dtl']) ? $v['club_dtl']['grade'] : '';
            $area = $v['identity']['area'] ? explode(',',$v['identity']['area']) : [];
            $area_arr = TeacherArea::get_arr();
            $area_name = '';
            foreach ($area as $kk=>$vv){
                $area_name .= isset($area_arr[$vv]) ? $area_arr[$vv].',' : '';
            }
            $arr[$k]['area_name'] = trim($area_name,',');
            $arr[$k]['tag_me']    = $v['tag_me'];
            
            // 检查是否收藏：1已收藏，2未收藏
            $is_collected = 2; // 默认2=未收藏
            if ($current_user_id && isset($v['id'])) {
                $collection = TeacherCollection::where(['user_id' => $current_user_id, 'teacher_detail_id' => $v['id']])->find();
                if ($collection) {
                    $is_collected = 1;
                }
            }
            $arr[$k]['is_collected'] = $is_collected; // 1已收藏，2未收藏
        }
        $count = $query_res->total();
        $data['teacher_list'] = $arr;
        $data['teacher_count'] = $count;

        $this->success('社团主页',$data);

        exit;
        $query_res = \app\admin\model\IdentityInfo::with(['user','clubDtl'])->where(['club_two_id'=>$club_dtl_id,'identity_type'=>'教师']);
        if(isset($params['tag'])&&$params['tag']){
            $query_res->whereRaw("FIND_IN_SET({$params['tag']}, tag) > 0");
        }
        $query_res = $query_res->order('sort','desc')
            ->paginate($params['page_size']);

        $list = $query_res->getCollection();
        $arr = [];
        foreach ($list as $k=>$v){
            $arr[$k]['id']     = $v['id'];
            $arr[$k]['user_id']     = $v['user_id'];
            $arr[$k]['name']     = $v['name'];
            $arr[$k]['club_dtl_name'] = $v['club_dtl']['name'];
            $arr[$k]['hourly_rate']    = $v['hourly_rate'];
            $arr[$k]['score']    = $v['score'];
            $arr[$k]['people']    = $v['people'];
            $arr[$k]['appointment_time_week']    = $v['appointment_time_week'].' '.$v['appointment_time'];
            $arr[$k]['avatar']    = $v['user']['avatar'];

            $arr[$k]['stage'] = isset($v['club_dtl']) ? $v['club_dtl']['stage'] : '';
            $arr[$k]['grade'] = isset($v['club_dtl']) ? $v['club_dtl']['grade'] : '';
            $area = $v['area'] ? explode(',',$v['area']) : [];
            $area_arr = TeacherArea::get_arr();
            $area_name = '';
            foreach ($area as $kk=>$vv){
                $area_name .= isset($area_arr[$vv]) ? $area_arr[$vv].',' : '';
            }
            $arr[$k]['area_name'] = trim($area_name,',');
            $arr[$k]['tag_me']    = $v['tag_me'];
        }
        $count = $query_res->total();
        $data['teacher_list'] = $arr;
        $data['teacher_count'] = $count;

        $this->success('社团主页',$data);
    }
}
